package net.ojbk.sso.center.web.handlers;

import net.ojbk.sso.center.beans.po.User;
import net.ojbk.sso.center.beans.pojo.ClientWebSystem;
import net.ojbk.sso.center.service.ClientWebsiteService;
import net.ojbk.sso.core.clientWeb.handlers.ClientController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/3/31
 * Time: 12:26
 * Description: 用户维护ClientWebsites的Controller
 * ！！ 拦截器已拦截未登陆的用户
 */
@Controller
@RequestMapping(value = "/cwebs")
public class ClientWebsiteController {
    private static Logger logger = Logger.getLogger(ClientController.class);
    @Autowired
    private ClientWebsiteService websiteService;
    @Autowired
    private ClientWebSystem clientWebSystem;

    // 0 工具类
    private void checkUid(HttpSession session, String uid) throws Exception{
        // 判断uid是否与session的uid一致
        User user = (User) session.getAttribute("user");
        if (!uid.equals(user.getUid()))
            throw new Exception("uid与地址不匹配。");
    }

    // 1.1 获取该用户所有在籍的子网站
    @RequestMapping(value = "{uid}", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllMyWebsite(HttpSession session, @PathVariable("uid") String uid) throws Exception {
        checkUid(session, uid);
        return new ResponseEntity<>(websiteService.getAllWebsite_ByUid(uid), HttpStatus.OK);
    }

    // 1.2 注册一个新的ClientWebsite
    @RequestMapping(value = "{uid}", method = RequestMethod.POST, params = {"cname", "clientUrl"})
    public ResponseEntity<Object> registerNewClientWeb(HttpSession session, @PathVariable("uid") String uid, String cname, String clientUrl) throws Exception {
        checkUid(session, uid);
        clientWebSystem.registerNewWebsite(uid, cname, clientUrl);
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    // 1.3 设置新的IP，如果newIp不存在，则自动获取
    @RequestMapping(value = "{uid}/{cname}", method = RequestMethod.PUT)
    public ResponseEntity<Object> updateIp(HttpSession session, @PathVariable("uid") String uid,@PathVariable("cname") String cname, String newIp) throws Exception {
        checkUid(session, uid);
        logger.debug(uid + " wants to change the " + cname + "'s ip to " + newIp );
        // 权限！此clientWebsite是否为该uid的。
        if (!clientWebSystem.isUsersWeb(uid, cname))
            throw new Exception("The clientWebsite of cname is not your.");

        if (newIp == null || newIp.equals(""))
            clientWebSystem.updateWebIp(cname);
        else
            clientWebSystem.updateWebIp(cname, newIp);

        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    // 1.4 注销这个clientWebsite
    @RequestMapping(value = "{uid}/{cname}", method = RequestMethod.DELETE)
    public ResponseEntity<Object> deleteWeb(HttpSession session, @PathVariable("uid") String uid,@PathVariable("cname") String cname) throws Exception {
        checkUid(session, uid);
        if (!clientWebSystem.isUsersWeb(uid, cname))
            throw new Exception("The clientWebsite of cname is not your.");
        clientWebSystem.deleteWeb(cname);
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

}
